package com.clone.surpassli.clone_cloudreader.ui.gank.child;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.clone.surpassli.clone_cloudreader.R;
import com.clone.surpassli.clone_cloudreader.adapter.AndroidAdapter;
import com.clone.surpassli.clone_cloudreader.app.Constants;
import com.clone.surpassli.clone_cloudreader.base.BaseFragment;
import com.clone.surpassli.clone_cloudreader.bean.GankIoDataBean;
import com.clone.surpassli.clone_cloudreader.databinding.FragmentCustomBinding;
import com.clone.surpassli.clone_cloudreader.http.RequestImpl;
import com.clone.surpassli.clone_cloudreader.http.cache.ACache;
import com.clone.surpassli.clone_cloudreader.model.GankOtherModel;
import com.clone.surpassli.clone_cloudreader.utils.DebugUtil;
import com.clone.surpassli.clone_cloudreader.utils.SPUtils;
import com.clone.surpassli.library.HttpUtils;
import com.cocosw.bottomsheet.BottomSheet;

import rx.Subscription;

/**
 * Created by SurpassLi on 2017/10/13.
 * CustomFragment 干货定制Fragment
 */

public class CustomFragment extends BaseFragment<FragmentCustomBinding> {

    private static final String TAG = "CustomFragment";
    private static final String TYPE = "mType";
    private String mType = "all";
    private int mPage = 1;
    private boolean mIsPrepared;
    private boolean mIsFirst = true;
    private AndroidAdapter mAndroidAdapter;
    private ACache mACache;
    private GankIoDataBean mAllBean;
    private View mHeaderView;
    private GankOtherModel mModel;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        DebugUtil.error("----onActivityCreated ----CustomFragment");
        mACache = ACache.get(getContext());
        mModel = new GankOtherModel();
        mAndroidAdapter = new AndroidAdapter();
        mIsPrepared = true;
    }

    @Override
    protected void loadData() {
        if (!mIsPrepared || !mIsVisible || !mIsFirst) {
            return;
        }
        if (mAllBean != null && mAllBean.getResults() != null && mAllBean.getResults().size() > 0) {
            showContentView();
            mAllBean = (GankIoDataBean) mACache.getAsObject(Constants.GANK_CUSTOM);
        } else {
            loadDataCustom();
        }
    }

    private void loadDataCustom() {
        mModel.setData(mType, mPage, HttpUtils.per_page_more);
        mModel.getGankIoData(new RequestImpl() {
            @Override
            public void loadSuccess(Object object) {
                showContentView();
                GankIoDataBean gankIoDataBean = (GankIoDataBean) object;
                if (mPage == 1) {
                    if (gankIoDataBean != null && gankIoDataBean.getResults() != null && gankIoDataBean.getResults().size() > 0) {
                        setAdater(gankIoDataBean);
                        mACache.remove(Constants.GANK_CUSTOM);
                        // 缓存50分钟
                        mACache.put(Constants.GANK_CUSTOM, gankIoDataBean, 30000);
                    }
                } else {
                    if (gankIoDataBean != null && gankIoDataBean.getResults() != null && gankIoDataBean.getResults().size() > 0) {
                        mAndroidAdapter.addAll(gankIoDataBean.getResults());
                        mAndroidAdapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void loadFailed() {
                showContentView();
                if (mAndroidAdapter.getItemCount() == 0) {
                    showError();
                }
                if (mPage > 1) {
                    mPage--;
                }
            }

            @Override
            public void addSubscription(Subscription subscription) {
                CustomFragment.this.addSubscription(subscription);
            }
        });
    }

    private void setAdater(GankIoDataBean mAllBean) {
        initHeader();
    }

    private void initHeader() {
        bindingView.txName.setText(SPUtils.getString("gank_cala", "全部"));
        bindingView.llChooseCatalogue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new BottomSheet.Builder(getActivity(), R.style.BottomSheet_StyleDialog).title("选择分类").sheet(R.menu.gank_bottomsheet).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case R.id.gank_all:
                                if (isOtherType("全部")) {
                                    bindingView.txName.setText("全部");
                                    mType = "all";
                                    mPage = 1;
                                    mAndroidAdapter.clear();
                                    SPUtils.putString("gank_cala", "全部");
                                    showLoading();
                                    showContentView();
                                }
                                break;
                            case R.id.gank_ios:
                                if (isOtherType("IOS")) {
                                    bindingView.txName.setText("IOS");
                                    mType = "iOS";
                                    mPage = 1;
                                    mAndroidAdapter.clear();
                                    SPUtils.putString("gank_cala", "IOS");
                                    showLoading();
                                    showContentView();
                                }
                                break;
                            case R.id.gank_qian:
                                if (isOtherType("前端")) {
                                    changeContent("前端");
                                }
                                break;
                            case R.id.gank_app:
                                if (isOtherType("App")) {
                                    changeContent("App");
                                }
                                break;
                            case R.id.gank_movie:
                                if (isOtherType("休息视频")) {
                                    changeContent("休息视频");
                                }
                                break;
                            case R.id.gank_resouce:
                                if (isOtherType("扩展资源")) {
                                    changeContent("扩展资源");
                                }
                                break;
                        }
                    }
                }).show();
            }
        });
    }

    public void changeContent(String content) {
        bindingView.txName.setText(content);
        mType = content;
        mPage = 1;
        mAndroidAdapter.clear();
        SPUtils.putString("gank_cala", content);
        showLoading();
        showContentView();
    }

    public boolean isOtherType(String type) {
        if (SPUtils.getString("gank_cala", "全部").equals(type)) {
            Toast.makeText(getContext(), "当前已经是" + type + "分类", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public int setContent() {
        return R.layout.fragment_custom;
    }

    @Override
    public void onResume() {
        super.onResume();
        DebugUtil.error("---onResume -----CustomFragment");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DebugUtil.error("----onDestroy ----CustomFragment");
    }
}
